package com.budwk.app.base.utils.nutzBeanUtils;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

/**
 * @author ZY.Wang
 * @since 2023/8/9
 */
public class NutzIocUtils implements Setup {

    public static String tokenForFastDFSFile = "";

    public static Ioc ioc;

    public static void setAToken(String tokenValue) {
        tokenForFastDFSFile = tokenValue;
    }

    public void init(NutConfig conf) {
        NutzIocUtils.ioc = conf.getIoc();
    }

    @Override
    public void destroy(NutConfig nutConfig) {

    }

    public static Ioc getIoc() {
        return ioc;
    }

    public static Object getBeanByType(Class<?> clazz) {
        return ioc.getByType(clazz);
    }

    public static PropertiesProxy getPropertiesProxy() {
        return ioc.getByType(PropertiesProxy.class);
    }

    public static <T> T getBean(Class<T> clazz) {
        return ioc.getByType(clazz);
    }

    public static String getDownloadFileCompleteUrl(String url) {
        return getDownloadFileCompleteUrl(url, true);
    }

    public static String getDownloadFileCompleteUrl(String url, boolean tokenFlag) {
        PropertiesProxy propertiesProxy = getPropertiesProxy();
        if (tokenFlag) {
            // return propertiesProxy.get("fastDfs.ip") + propertiesProxy.get("fastDfs.downloadRest") + url + "?auth_token=" + tokenForFastDFSFile;
            return propertiesProxy.get("fastDfs.ip") + url + "?auth_token=" + tokenForFastDFSFile;
        } else {
            // return propertiesProxy.get("fastDfs.ip") + propertiesProxy.get("fastDfs.downloadRest") + url;
            return propertiesProxy.get("fastDfs.ip") + url;
        }
    }
}
